<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>配置审批流</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../css/jquery.jsonview.css">
    <script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/commonutil.js"></script>
    <script type="text/javascript" src="../js/jquery.jsonview.js"></script>
    <script src="./function.js" charset="utf-8"></script>
</head>
<body>
<div class="edit-form-class" style="padding-top: 20px">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" id="configAuditForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">模板名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateName" name="templateName" lay-verify="title" readonly="readonly"
                               style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
                        <input type="text" id="templateNo" name="templateNo" style="display:none;" autocomplete="off">
                        <input type="text" id="id" name="id" style="display:none;" autocomplete="off">
                        <input type="text" id="approvalId" name="approvalId" style="display:none;" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">模板来源</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateResourceName" name="templateResourceName" lay-verify="title"
                               readonly="readonly" style="width: 350px" autocomplete="off" placeholder=""
                               class="layui-input">
                        <input type="text" id="templateResource" name="templateResource" lay-verify="title"
                               readonly="readonly" style="width: 350px;display: none;" autocomplete="off" placeholder=""
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">模板文件</label>
                    <div class="layui-input-inline" style="padding-top: 8px;padding-left: 10px">
                        <a href="#" id="templateName2"></a>
                        <input type="text" id="tempRefId" name="tempRefId" style="display:none;" autocomplete="off">
                    </div>
                    <input id='templateExtId' name='templateExtId' type="text" class="text" maxlength='50'
                           style='display:none;'/>
                    <span id="fileName" style="color:#999"></span>
                    <button type="button" class="layui-btn layui-btn-normal" style='display:none;' id="uploadFile">
                        重新上传
                    </button>
                    </br></br>
                    <span style="color:#999">只能上传ftl格式文件，文件不能超过100kb</span>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">签章类型</label>
                    <div class="layui-input-block">
                        <input type="text" id="sealTypeName" name="sealTypeName" lay-verify="title" readonly="readonly"
                               style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
                        <input type="text" id="sealType" name="sealType" lay-verify="title" readonly="readonly"
                               style="width: 350px;display: none;" autocomplete="off" placeholder=""
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公章规则</label>
                    <div class="layui-input-block" id="officialSealId" style="width: 350px">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">配置审批流</label>
                    <div class="layui-input-block" id="configAudit" style="width: 350px">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block" style="width: 350px">
                        <textarea placeholder="请输入内容" class="layui-textarea" id="remark" name="remark" lay-verify="remarksVerify"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="btnclass" style="padding-left: 200px;">
                        <div class="layui-btn" lay-submit="" autocomplete="off" lay-filter="submitBtn"
                             id="configAuditBtn">提交申请
                        </div>
                        <div id="closeWin" class="layui-btn layui-btn-primary">取消</div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['laypage', 'jquery', 'layer', 'table', 'form', 'util', 'upload', 'layedit'], function () {
        let form = layui.form;
        let upload = layui.upload;

        let id = GetUrlParam("id");
        let url = serverPre + "/manager/scs/v2/template/one/" + id;
        let result = requestGet(url);
        //初始化数据
        configAuditWin(result, form);
        $("#configAudit").load("../approval/approvalConfig.html", "", function () {
            var data = getApprovalDetail(result.approvalId)
            if (data == null) {
                approval = initSubmitApproval("开始");
            } else {
                approval = initApprovalByDatas(initApproversByData(data));
            }
        })
        //文件上传
        let uploadUrl = serverPre + "/manager/scs/v2/template/upload";
        uploadFile(upload, uploadUrl);
        form.render();
        //下载文件
        $('#downLoadFile').click(function () {
            exportTemplate(result.refId);
        });
    })
</script>
<script>
    //取消
    $("#closeWin").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });
    //提交
    $("#configAuditBtn").click(function () {
        var approverObj = approval.result();
        let url = serverPre + "/manager/scs/v2/template/approval/update";
        let formData = serializeObject($("#configAuditForm"));
        let officialSealId = [];
        $("#officialSealId").find('input:checkbox').each(function (i, item) {
            if ($(this).is(':checked')) {
                officialSealId.push(item.value);
            }
        });
        let commitData = JSON.stringify({
            "id": formData.id,
            "resource": formData.templateResource,
            "remark": formData.remark,
            "templateName": formData.templateName,
            "refId": formData.templateExtId,
            "sealType": formData.sealType,
            "templateNo": formData.templateNo,
            "officialSealId": officialSealId,
            "userInfoRos": approverObj.userInfos,
            "approverIds": approverObj.approvers,
            "approvalId": formData.approvalId
        });
        saveTemplate(url, commitData);
    });
    //下载
    $("#templateName2").click(function () {
        let formData = serializeObject($("#configAuditForm"));
        let refId = formData.tempRefId;
        if (refId !== null && refId !== "") {
            exportTemplate(refId);
        } else {
            errorMsg("模板文件不存在！")
        }
    });

    /**
     * 配置审批流
     */
    function getApprovalDetail(approvalId) {
        var data = null;
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/saas/v1/approval/detail/" + approvalId,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0 && result.data) {
                    data = result.data
                }
            }
        });
        return data;
    }

    function initApproversByData(approvalDetail) {
        var approvers = [{userName: approvalDetail["applyUserName"], status: 1, isApplyUser: true}];
        approvalDetail.approvers.forEach(function (e) {
            approvers.push({ehrId: e.approverId, userName: e.approverName, status: e.approvalStatus});
        })
        return approvers;
    }
</script>
</html>
